<?php
namespace app\cls\sdk;
//require_once "/config.php";
//require_once "/FoundaoVideoSDKSign.class.php";
//require_once "/FoundaoVideoSDKParams.class.php";

class FoundaoVideoSDK extends FoundaoVideoSDKParams {
    private static $API_SUBMIT_JOB = FOUNDAO_VIDEO_BASE_APIS . "submitJob.php";
    private static $API_GET_MEDIA_INFO = FOUNDAO_VIDEO_BASE_APIS . "getMediaInfo.php";
    private static $API_GET_JOB_STATUS_INFO = FOUNDAO_VIDEO_BASE_APIS . "getJobStatus.php";
    private static $API_GET_JOB_GET_COVER = FOUNDAO_VIDEO_BASE_APIS . "getCover.php";

    public function __construct($appId = "", $appSecret = "") {
        if (empty($appId)) {
            $appId = FOUNDAO_VIDEO_APP_ID;
            $appSecret = FOUNDAO_VIDEO_APP_SECRET;
        }
        parent::__construct($appId, $appSecret);
    }

    public static function transcode($api, $appId, $appSecret, $inputUrl, $path) {
        $handle = new self($appId, $appSecret);
        $handle->createInputVideo($inputUrl);
        $handle->createOutput($path, "temp", "video");
        $params = $handle->getParams();
        $handle->submitJob($params);
    }

    /**
     * 功能描述：提交处理任务
     * @param bool $debug
     * @return mixed
     */
    public function submitJob($debug = false) {
        $params = $this->getParams();
        $this->clearParams();
        if ($debug == true) {
            var_dump($params);
        }
        $data = self::curlRequest(self::$API_SUBMIT_JOB, $params);
        if (!empty($data)) {
            return $data['data']['job_id'];
        } else {
            return false;
        }
    }

    /**
     * 功能描述：获取媒体信息
     * @param $url
     * @param string $type
     * @return mixed
     */
    public function getMediaInfo($url, $type = "immediate") {
        $videoUrl = array();
        if (!is_array($url)) {
            $videoUrl[] = $url;
        } else {
            $videoUrl = $url;
        }
        if ($type != "callback") {
            $type = "immediate";
        }
        $this->clearParams();
        $this->setParamsOption("video_urls", $videoUrl);
        $this->setParamsOption("type", $type);
        $params = $this->getParams();
        $this->clearParams();
        $mediaInfo = self::curlRequest(self::$API_GET_MEDIA_INFO, $params);
        if (!empty($mediaInfo)) {
            if (!is_array($url)) {
                return $mediaInfo['data'][0];
            } else {
                return $mediaInfo['data'];
            }
        }
        return false;
    }

    public function getCover($url) {
        $this->clearParams();
        $this->setParamsOption("video_url", $url);
        $params = $this->getParams();
        $this->clearParams();
        $content = self::_curlRequest(self::$API_GET_JOB_GET_COVER, $params);
        if (empty($content)) {
            $content = self::_curlRequest(self::$API_GET_JOB_GET_COVER, $params);
        }
        return $content;
    }

    /**
     * 功能描述：获取job的状态
     * @param $job_id
     * @return mixed
     */
    public function getJobStatus($job_id) {
        $this->clearParams();
        $this->setParamsOption("job_id", $job_id);
        $params = $this->getParams();
        $this->clearParams();
        $ret = self::curlRequest(self::$API_GET_JOB_STATUS_INFO, $params);
        if (!empty($ret)) {
            return $ret['data'];
        }
        return false;
    }

    /**
     * 功能描述：curl发送请求
     * @param $url
     * @param $data
     * @return bool|mixed
     */
    public static function curlRequest($url, $data) {
        $ret = "";
        for ($i = 0; $i < 3; $i++) {
            $ret = self::_curlRequest($url, $data);
            if (!empty($ret)) {
                $dd = @json_decode($ret, true);
                if (isset($dd['code']) && $dd['code'] === 0) {
                    return $dd;
                }
            }
        }
        echo "foundao video sdk curl send msg error " . $ret . "\n";
//        exit(1);
        return false;

    }

    /**
     * @param $url
     * @param $data
     * @return mixed
     */
    private static function _curlRequest($url, $data = null) {
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        if (!empty($data)) {
            curl_setopt($ch, CURLOPT_POST, 1);
            curl_setopt($ch, CURLOPT_TIMEOUT, 2);
            curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
        }
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
        $response = curl_exec($ch);
        if(empty($response)){
            var_dump(curl_error($ch));exit;
        }
        return $response;
    }
}